package com.itms.tool;

import java.util.ArrayList;
import java.util.List;

import android.app.Activity;
import android.app.AlertDialog;
import android.app.Dialog;
import android.content.DialogInterface;
import android.content.DialogInterface.OnClickListener;
import android.os.Bundle;
import android.os.Parcel;
import android.os.Parcelable;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.ListView;

import com.actionbarsherlock.app.SherlockDialogFragment;

public class TopicChooserDialogFragment extends SherlockDialogFragment {

	private TopicInfo[] mTopics;
	private ListView mTopicsListView;
	private TopicsChooseListener mListener;

	public static TopicChooserDialogFragment getInstance() {
		TopicInfo[] topics = new TopicInfo[6];
		for (int i = 0; i < topics.length; i++) {
			topics[i] = new TopicInfo(i + 1, "Topic-" + (i + 1));
		}
		return getInstance(topics);
	}

	public static TopicChooserDialogFragment getInstance(List<TopicInfo> topics) {
		return getInstance(topics.toArray(new TopicInfo[topics.size()]));
	}

	public static TopicChooserDialogFragment getInstance(TopicInfo[] topics) {
		Bundle args = new Bundle();
		args.putParcelableArray("topics", topics);
		TopicChooserDialogFragment fgmnt = new TopicChooserDialogFragment();
		fgmnt.setArguments(args);
		return fgmnt;
	}

	@Override
	public void onAttach(Activity activity) {
		super.onAttach(activity);
		Fragment f = getTargetFragment();
		if (f instanceof TopicsChooseListener) {
			mListener = (TopicsChooseListener) f;
		} else {
			throw new IllegalStateException("fragment: " + (f != null ? f.getClass() : "n/a") + 
					" is not " + TopicsChooseListener.class);
		}
	}

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		Bundle args = getArguments();
		if (args != null) {
			mTopics = (TopicInfo[]) args.getParcelableArray("topics");
		}
		if (mTopics == null) {
			mTopics = new TopicInfo[0];
		}
	}

	@Override
	public Dialog onCreateDialog(Bundle savedInstanceState) {
		return new AlertDialog.Builder(getActivity())
				.setView(createListView(savedInstanceState))
				.setTitle(R.string.choose_topics)
				.setPositiveButton(R.string.save, new OnClickListener() {

					@Override
					public void onClick(DialogInterface dialog, int which) {
						if (mListener != null) {
							List<TopicInfo> topics = new ArrayList<TopicInfo>();
							for (int i = 0; i < mTopics.length; i++) {
								if (mTopicsListView.isItemChecked(i)) {
									topics.add(mTopics[i]);
								}
							}
							mListener.onTopicsChosen(topics);
						}
					}
				})
				.setNegativeButton(R.string.cancel, null)
				.create();
	}

	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
		setCancelable(false);
		return super.onCreateView(inflater, container, savedInstanceState);
	}
	
	public ListView createListView(Bundle savedInstanceState) {
		mTopicsListView = new ListView(getActivity());
		mTopicsListView.setAdapter(new ArrayAdapter<TopicInfo>(getActivity(), R.layout.checkable_textview, mTopics));
		mTopicsListView.setChoiceMode(ListView.CHOICE_MODE_MULTIPLE);
		if (savedInstanceState != null) {
			boolean[] checkStates = savedInstanceState.getBooleanArray("check_states");
			for (int i = 0; i < checkStates.length; i++) {
				mTopicsListView.setItemChecked(i, checkStates[i]);
			}
		}
		return mTopicsListView;
	}

	@Override
	public void onSaveInstanceState(Bundle outState) {
		super.onSaveInstanceState(outState);
		int n = mTopics.length;
		boolean[] checkStatesArray = new boolean[n];
		for (int i = 0; i < n; i++) {
			checkStatesArray[i] = mTopicsListView.isItemChecked(i);
		}
		outState.putBooleanArray("check_states", checkStatesArray);
	}

	@Override
	public void onDetach() {
		super.onDetach();
		mListener = null;
	}

	public interface TopicsChooseListener {
		void onTopicsChosen(List<TopicInfo> topics);
	}

	public static class TopicInfo implements Parcelable {
		public static Parcelable.Creator<TopicInfo> CREATOR = 
				new Parcelable.Creator<TopicInfo>() {
	
				@Override
				public TopicInfo createFromParcel(Parcel source) {
					TopicInfo topic = new TopicInfo();
					topic.mId = source.readInt();
					topic.mName = source.readString();
					return topic;
				}
	
				@Override
				public TopicInfo[] newArray(int size) {
					return new TopicInfo[size];
				}
		};
		
		private int mId;
		private String mName;

		public TopicInfo() {

		}

		public TopicInfo(int id, String name) {
			mId = id;
			mName = name;
		}

		@Override
		public String toString() {
			return mName;
		}

		@Override
		public int describeContents() {
			return 0;
		}

		@Override
		public void writeToParcel(Parcel dest, int flags) {
			dest.writeInt(mId);
			dest.writeString(mName);
		}
	}
}
